<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="userList" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="50">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="110px" align="center" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatarUrl"  min-width="70" height="70" />
        </template>
      </el-table-column>

      <el-table-column width="110px" align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{ scope.row.name}}</span>
        </template>
      </el-table-column>

      <el-table-column width="170px" align="center" label="注册日期">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          {{ timestampToTime(scope.row.gmtCreate) }}
        </template>
      </el-table-column>

      <el-table-column width="170px" align="center" label="最近一次登录日期">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          {{ timestampToTime(scope.row.gmtModify) }}
        </template>
      </el-table-column>

      <el-table-column width="110" class-name="status-col" align="center" label="登录方式">
        <template slot-scope="{row}">
          <el-tag type="info">
            {{ chaekMode(row.mode) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column width="110" class-name="status-col" align="center" label="状态">
        <template slot-scope="{row}">
          <el-tag :type=" chaekStatus(row.status) | statusFilter">
            {{ chaekStatus(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="300" class-name="status-col" align="center" label="个签">
        <template slot-scope="{row}">
         {{row.bio}}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==1" type="danger" size="small" icon="el-icon-error" @click="disable(scope)">
            禁用
          </el-button>
          <el-button v-if="scope.row.status==0" type="primary" size="small" icon="el-icon-error" @click="disable(scope)">
            取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchAllUserList, disableUser } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'UserList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        未禁用: '',
        禁用: 'danger'
      }
      return statusMap[status]
    }
  },
  // 数据
  data() {
    return {
      userList: null, // 用户集合
      total: 0, // 用户总数
      listLoading: true, // 开始/结束加载
      listQuery: { // 分页的条件
        page: 1,
        limit: 10
      }
    }
  },
  // 生命钩子函数
  created() {
    // 获取用户列表
    this.getUserList()
  },
  methods: {
    // 获取前台用户集合
    getUserList() {
      this.listLoading = true
      fetchAllUserList(this.listQuery).then(response => {
        this.userList = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 禁用用户
    disable({ $index, row }) {
      const id = row.id
      disableUser(id).then(res => {
        if (res.data == true) {
          this.userList[$index].status = this.userList[$index].status == 0 ? 1 : 0
          this.$message(this.userList[$index].status == 0 ? '禁用成功！' : '解除禁用成功！')
        } else {
          this.$message.error('禁用失败！')
        }
      })
    },
    // 验证用户状态
    chaekStatus(status) {
      const statusMap = {
        0: '禁用',
        1: '未禁用'
      }
      return statusMap[status]
    },
    chaekMode(mode) {
      const modeMap = {
        0: 'GitHub',
        1: 'Gitee',
        2: 'QQ'
      }
      return modeMap[mode]
    },
    // 根据时间戳获取正确的时间格式
    timestampToTime(timestamp) {
      var date = new Date(timestamp);   //创建一个指定的日期对象
      var year = date.getFullYear();  //取得4位数的年份
      var month = date.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
      var day = date.getDate();      //返回日期月份中的天数（1到31）
      var hour = date.getHours();     //返回日期中的小时数（0到23）
      var minute = date.getMinutes(); //返回日期中的分钟数（0到59）
      var second = date.getSeconds(); //返回日期中的秒数（0到59）
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }
      if (hour < 10) {
        hour = '0' + hour;
      }
      if (minute < 10) {
        minute = '0' + minute;
      }
      if (second < 10) {
        second = '0' + second;
      }
      return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
  }
}
</script>

// 样式表
<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
